﻿/*
 * 由SharpDevelop创建。
 * 用户： Admin
 * 日期: 2025/5/5
 * 时间: 2:12
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading;

namespace LogSaver
{
	/// <summary>
	/// addonJs输出信息存储器，仅保存[Scripting]系列信息
	/// </summary>
	public class LogSaver
	{
		public BDSCSBox.TYPES.F_FUNC onreceive = null;
		
		public const string event_file_path = "event.txt";
		
		public static LogSaver instance = null;

		public Mutex mutex = new Mutex();
		
		/// <summary>
		/// 添加一条监控信息到文件
		/// </summary>
		/// <param name="s"></param>
		public void eventAdd(string s)
		{
			mutex.WaitOne();
			File.AppendAllLines(event_file_path, new string[]{ s });	// 逐行存储，不设限制，通过计划任务进行每日日志调度
			mutex.ReleaseMutex();
		}

		private LogSaver(BDSCSBox.DataAdapter h) {
			
			onreceive = (s) =>{
					if (s.IndexOf("[Scripting]") > -1)
						eventAdd(s);
				return true;
			};
			
			h.addDataReceiver(onreceive);
		}
		
		public static void getInstance(BDSCSBox.DataAdapter h) {
			instance = (instance ?? new LogSaver(h));
		}
	}
}

namespace BDSCSBox {
	public class CSL {
		public bool onStart(BDSCSBox.DataAdapter handle) {	// dll入口，注册所有函数
			LogSaver.LogSaver.getInstance(handle);
			Console.WriteLine("[LogSaver] AddonJs日志记录已加载。");
			return true;
		}
	}
}